package com.zzzyh.forumsystemclient.services.impl;

import com.zzzyh.forumsystemclient.dao.MomentMapper;
import com.zzzyh.forumsystemclient.dao.UserMapper;
import com.zzzyh.forumsystemclient.model.Moment;
import com.zzzyh.forumsystemclient.model.User;
import com.zzzyh.forumsystemclient.services.IMomentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MomentServiceImpl implements IMomentService {
    @Autowired
    private MomentMapper momentMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<Moment> list(Integer userId ,Integer pageNum, Integer pageSize) {
        List<Integer> friends = null;
        if (userId == 1){
            friends = userMapper.getAdminFriendsList();
        }else{
           friends = userMapper.getFriends(userId);
           friends.add(1);
        }
        friends.add(userId);
        List<Moment> moments = momentMapper.list(friends, (pageNum - 1) * pageSize, pageSize);
        for (Moment moment : moments){
            User user = userMapper.selectByPrimaryKey(Math.toIntExact(moment.getUserId()));
            moment.setNickname(user.getNickname());
            moment.setAvatar(user.getAvatar());
        }
        return moments;
    }

    @Override
    public Integer total(Integer userId ) {
        List<Integer> friends = userMapper.getFriends(userId);
        friends.add(userId);
        return momentMapper.total(friends);
    }

    @Override
    public void add(Moment moment) {
        momentMapper.add(moment);
    }

    @Override
    public void update(Moment moment) {
        momentMapper.update(moment);
    }
}
